<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<link rel="stylesheet" media="screen" href="${ctx}/css/Tree.css"/>
<!-- jQplot CSS END -->

<script type="text/javascript" src="${ctx}/js/jquery.itextclear.js"></script>
<script type="text/javascript" src="${ctx}/js/Tree.js"></script>
<script type="text/javascript">
    $(document)
            .ready(
            function () {
                $(
                        'input[type=text], input[type=password], input[type=url], input[type=email], input[type=number], textarea',
                        '.form').iTextClear();
            }
    );
</script>
<!-- jQplot CSS -->


<!-- DATATABLES CSS -->
<link rel="stylesheet" media="screen"
      href="${ctx}/lib/datatables/css/vpad.css"/>
<script type="text/javascript"
        src="${ctx}/lib/datatables/js/jquery.dataTables.js"></script>

<!-- DATATABLES CSS END -->


<h1 class="page-title">招聘安排 - ${title}</h1>

<div class="container_12 clearfix leading">
    <div class="side grid_12 leading">
        <form class="form has-validation" id="post_form">
            <s:if test="#request.inviteArrange != null">
                <input type="hidden" name="inviteArrange.id" value="${inviteArrange.id}"/>
            </s:if>
            <div class="clear"></div>
            <div class="clearfix">
                <label class="form-label">需求单位 <em>*</em></label>

                <div class="form-input">
                    <select id="org_sel">
                        <s:iterator value="#request.organizations" var="org" status="st">
                            <s:if test="#st.index == 0 && #request.inviteArrange == null">
                                <option value="${org.id}" selected="selected">${org.path}</option>
                            </s:if>
                            <s:elseif test="#request.inviteArrange.inviteApply.organization.id == #org.id">
                                <option value="${org.id}" selected="selected">${org.path}</option>
                            </s:elseif>
                            <s:else>
                                <option value="${org.id}">${org.path}</option>
                            </s:else>
                        </s:iterator>
                    </select>
                </div>
            </div>
            <div class="clearfix">
                <label  class="form-label">招聘岗位</label>

                <div class="form-input">
                    <select name="inviteArrange.inviteApply.id" id="invite_sel">
                        <s:iterator value="#request.inviteApplies" var="invite" status="st">
                            <s:if test="#st.index == 0 && #request.inviteArrange == null">
                                <option value="${invite.id}" selected="selected">${invite.duty.dutyName}</option>
                            </s:if>
                            <s:elseif test="#request.inviteArrange.inviteApply.id == #invite.id">
                                <option value="${invite.id}" selected="selected">${invite.duty.dutyName}</option>
                            </s:elseif>
                            <s:else>
                                <option value="${invite.id}">${invite.duty.dutyName}</option>
                            </s:else>
                        </s:iterator>
                    </select>
                </div>
                <div class="clearfix">
                </div>
            </div>
            <div class="clearfix">
                <label class="form-label">招聘渠道</label>

                <div class="form-input">
                    <select name="inviteArrange.inviteFashion.id" id="fashion_sel">
                        <s:iterator value="#request.inviteFashions" var="fashion" status="st">
                            <s:if test="#st.index == 0 && #request.inviteArrange == null">
                                <option value="${fashion.id}" selected="selected">${fashion.channel}</option>
                            </s:if>
                            <s:elseif test="#request.inviteArrange.inviteFashion.id == #fashion.id">
                                <option value="${fashion.id}" selected="selected">${fashion.channel}</option>
                            </s:elseif>
                            <s:else>
                                <option value="${fashion.id}">${fashion.channel}</option>
                            </s:else>

                        </s:iterator>
                    </select>
                </div>
                <div class="clearfix">
                </div>
            </div>


            <div class="form-action clearfix">
                <button class="button" type="button" onclick="checkForm()">
                    <s:if test="#request.inviteApply.id > 0">修改</s:if>
                    <s:else>添加</s:else>
                </button>
                <button class="button" type="reset">重置</button>
                <a class="button" href="javascript:void(0)"
                   onclick="javascript:history.back()"><span
                        style="background: url(${ctx}/images/img/arrow_undo.png)" class="icon">&nbsp;</span>返回</a>
            </div>
        </form>
    </div>
</div>
<div class="clear"></div>
<script type="text/javascript">
    $(document).ready(function () {
        //bind event to org_sel
        $("#org_sel").change(function () {
            getInviteApplysByOrgId($("#org_sel").val());
        })
    });

    /**
     * 根据组织架构编号获取申请招聘职位列表
     */
    function getInviteApplysByOrgId(orgId) {
        $.getJSON("${ctx}/ajax/apply_list.xhtml?organizationId=" + orgId,
                function (applys) {
                    var selObj = $("#invite_sel");
                    selObj.prev().html("");
                    selObj.html("");
                    if (applys != null && applys.length != 0) {
                        $.each(applys, function (i, apply) {
                            selObj.append("<option value='" + apply.id + "'>"
                                    + apply.dutyName + "</option>")
                        });
                        selObj.get(0).selectedIndex = 1;
                        selObj.prev().html($("#invite_sel option:selected").text());
                    }
                });
    }

    /**
     * 返回页面
     */
    function returnPage() {
        this.location.href = "#invite/invite_apply_list.xhtml?org_id="
                + $("#org_sel").value();
    }

    function checkForm() {
        if ($("#org_sel").children().length == 0) {
            alert("未选择组织结构，请先添加组织结构");
            return;
        }
        if ($("#invite_sel").children().length == 0) {
            alert("该组织结构没有招聘职务，请先申请招聘职务");
            return;
        }
        if ($("#fashion_sel").children().length == 0) {
            alert("请先填写招聘渠道");
            return;
        }

        //alert($("#post_form").serialize());
        var url = "${ctx}/invite/post_arrange.xhtml";
        $.ajax(url, {
            type:"POST",
            data:$("#post_form").serialize(),
            dataType:"html",
            cache:false,
            success:function (a) {
                if (confirm('还要继续添加吗？')) {
                    document.getElementById("post_form").reset();
                } else {
                    location.href = "${ctx}/index.xhtml#invite/invite_list.xhtml?p=invite_arrange";
                }
            },
            error:function (XMLHttpRequest, textStatus, errorThrown) {
                alert('提交申请发生异常');
            },
            complete:function () {
            }
        });
    }
</script>
